package com.example.mobile1931605066wzq0604;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
//创建服务，定义一个Service的子类
public class MusicService extends Service {
    String TAG="TAG";
    MediaPlayer mediaPlayer;

    class ServiceBinder extends Binder{
        public MusicService getMusicService(){
            return MusicService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: "+add(1,1));
        playMusic();
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: ");
        return new ServiceBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
        stopMusic();
    }
    public int add(int a,int b){
        return a+b;
    }
    public void playMusic(){
//        mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.old_boy);
//        mediaPlayer.start();
        if (mediaPlayer==null){
            mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.old_boy);
            mediaPlayer.start();
//        }else {
//            mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.old_boy);
//      mediaPlayer.start();
        }mediaPlayer.start();

    }
    public void pauseMusic(){
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }
    }
    public void  rePlayMusic(){
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }
    }
    public  void stopMusic(){
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer=null;
        }
    }

}
